var express = require('express');
var router = express.Router();


// 引入模块 
const fs = require('fs');
const path = require('path')

//渲染login
router.get('/login', (req, res) => {

  res.render('login')
})
router.post('/login', (req, res) => {
    // 接参
    let user = req.body;
    console.log(user);

    // 获取persons.json数据
    let dir = path.join(__dirname, 'persons.json');
    let data = fs.readFileSync(dir, 'utf-8');
    data = JSON.parse(data);

    // 判断
    let s = data.find(item => item.username == user.username&&item.password==user.password);

    if (s) {
        // 登录成功  存储用户信息  回应登录成功

        res.render('dlcg')

    } else {
    
        res.render('dlsb')

    }

    // 
})

module.exports = router;
